package com.shiguiwu.springmybatis.spring.aop.cglib;

import org.springframework.cglib.proxy.Dispatcher;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.LazyLoader;

import java.util.UUID;

/**
 * @description: dispatcher
 * @author: stone
 * @date: Created by 2021/5/18 20:38
 * @version: 1.0.0
 * @pakeage: com.shiguiwu.springmybatis.spring.aop.cglib
 */
public class DispatcherTests {

    public static class User {
        private String name;

        public User() {
        }

        public User(String name) {
            this.name = name;
        }

        void say() {
            System.out.println("你好：" + name);

        }
    }


    public static void main(String[] args) {
        Enhancer enhancer = new Enhancer();

        enhancer.setSuperclass(User.class);

        enhancer.setCallback(new Dispatcher() {
            @Override
            public Object loadObject() throws Exception {
                System.out.println("Dispatcher调用loadObject方法");
                return new User("石贵武" + UUID.randomUUID().toString());
            }

        });

        Object proxy = enhancer.create();
        User user = (User) proxy;

        //第一次调用
        user.say();

        //第二次调用
        user.say();
    }


}
